﻿using ConfExpert.Forms.Base;
using DevExpress.Data;
using DevExpress.XtraGrid.Columns;
using DevTeam;
using System;

namespace ConfExpert.Forms.Planlama
{
    public partial class ModelhaneBM : XForm
    {
        public ModelhaneBM()
        {
            InitializeComponent();
        }

        private void ModelhaneBM_Load(object sender, EventArgs e)
        {
            
            gridView1.Init("GetBMs");
            gridView1.AddDetailGrid(gridView2, "CustomerId");
            gridView2.AddDetailGrid(gridView3, "CustomerId,BrandId,Model");

            gridView3.MakeColumnInvisible("LineTotal", "Total2", "CustomerId", "BrandId", "Model");
            gridView2.MakeColumnInvisible("CustomerId");

            gridView2.AddColumn("OrtBM", typeof(decimal), "SUM(Child({FK}).LineTotal)/SUM(Child({FK}).Total)");
            gridView2.AddColumn("Total", typeof(decimal), "SUM(Child({FK}).Total2)");
            gridView1.AddColumn("Total", typeof(decimal), "SUM(Child({FK}).Total)");
            gridView1.AddColumn("ModelCnt", typeof(int), "COUNT(Child({FK}).Model)");

            gridView1.FormatAsNumber(0, "ModelCnt", "Total");
            gridView2.FormatAsNumber(0, "Total");
            gridView2.FormatAsNumber(3, "OrtBM");
            gridView3.FormatAsNumber(3, "OrtBM");
            gridView3.FormatAsDate("DosyaTeslimTarihi", "Tarih");

            gridView1.AddSummary("ModelCnt", "Total");
            gridView2.AddSummary("Total");
            gridView3.AddSummary();

            gridView1.MakeRowSelectable();
            gridView2.MakeRowSelectable();
            gridView3.MakeRowSelectable();

            gridView1.SortInfo.ClearAndAddRange(new GridColumnSortInfo[] { new GridColumnSortInfo(gridView1.Columns["Total"], ColumnSortOrder.Descending)});

            gridView1.BestFitColumns();
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            gridView2.FocusedRowChanged += gridView2_FocusedRowChanged;
            gridView1.FocusedRowHandle = 0;
        }

        void gridView2_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView3.BestFitColumns();
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
            gridView3.BestFitColumns();
        }
    }
}
